#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
 @Author: Y-aong
 @FileName: base_runner.py
 @DateTime: 2024/5/19 16:36
 @Version : 3.10
 @Description: 
"""
from abc import ABC

from orderlines.execute.app_context import AppContext
from orderlines.execute.process_parse import TaskPatternContext


class BaseRunner(ABC):
    def __init__(self, process_instance_id: str, context: AppContext):
        self.process_instance_id = process_instance_id
        self.context = context
        self.process_parse: TaskPatternContext = TaskPatternContext(process_instance_id, context)
        self.current_task_id = self.process_parse.consumer_task()
        self.message_id = self.process_parse.message_id()
        self.process_info = self.context.get_process_info(self.process_instance_id)
        self.task_nodes = self.context.get_task_nodes(self.process_instance_id)
        self.is_complete = True  # 是否完成，False为队列为空，完成

    @property
    def process_name(self):
        return self.context.get_process_item(self.process_instance_id, 'process_name')

    @property
    def process_id(self) -> str:
        return self.context.get_process_item(self.process_instance_id, 'process_id')

    def task_instance_id(self):
        return self.context.get_task_node_item(self.process_instance_id, self.current_task_id, 'task_instance_id')

    def task_name(self):
        return self.context.get_task_node_item(self.process_instance_id, self.current_task_id, 'task_name')

    def current_node(self):
        task_kwargs: dict = self.process_parse.task_parse.task_kwargs(self.current_task_id)
        task_config = self.process_parse.task_parse.task_config(self.current_task_id)
        current_task_node = self.context.get_task_node(self.process_instance_id, self.current_task_id)
        current_task_node['task_kwargs'] = task_kwargs
        current_task_node['task_config'] = task_config
        current_task_node['message_id'] = self.message_id
        current_task_node['process_name'] = self.process_name
        return current_task_node
